package lv.imho.utils;

import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.appengine.api.datastore.Text;
import com.google.gwt.user.client.Window.Navigator;

import lv.imho.client.model.DataSourceTransferObject;
import lv.imho.client.model.DataTransferObject;
import lv.imho.server.model.Data;
import lv.imho.server.model.DataSource;

/**
 * Common useful functions.
 * 
 * @author Igors Gulbinskis
 */
public class Utils {
			
	private static String getHTML(String text, String neutralPhrases[], String[] positivePhrases,
		Double[] positiveSemanticOrientations, String[] negativePhrases, Double[] negativeSemanticOrientations) {
		String result = text;
		String phrase = "";
		
		Pattern pattern;
		Matcher matcher;
		if (neutralPhrases != null) {
			for (int i = 0; i < neutralPhrases.length; i++) {
				pattern = Pattern.compile(neutralPhrases[i], Pattern.CASE_INSENSITIVE);
				matcher = pattern.matcher(result);
				if (matcher.find()) {
					phrase = result.substring(matcher.start(), matcher.end());
				}			
				result = matcher.replaceAll("<span style=\"background-color: blue; color: white;\">" + phrase + "</span>");
			}
		}
		
		DecimalFormat formatter = new DecimalFormat("#.###");
		String        semanticOrientation;		
		if (positivePhrases != null) {			
			for (int i = 0; i < positivePhrases.length; i++) {
				pattern = Pattern.compile(positivePhrases[i], Pattern.CASE_INSENSITIVE);
				matcher = pattern.matcher(result);
				if (matcher.find()) {
					phrase = result.substring(matcher.start(), matcher.end());
				}
								
				if (positiveSemanticOrientations != null
					&& positiveSemanticOrientations[i] != null) {
					semanticOrientation = formatter.format(positiveSemanticOrientations[i]);
				} else {
					semanticOrientation = "?";
				}
				
				result = matcher.replaceAll("<span style=\"background-color: green; color: white;\">"
					+ phrase + "<span style=\"vertical-align: super; font-size: x-small;\"><i>" + semanticOrientation + "</i>&nbsp;</span></span>");
			}
		}
		if (negativePhrases != null) {
			for (int i = 0; i < negativePhrases.length; i++) {
				pattern = Pattern.compile(negativePhrases[i], Pattern.CASE_INSENSITIVE);
				matcher = pattern.matcher(result);
				if (matcher.find()) {
					phrase = result.substring(matcher.start(), matcher.end());
				}
								
				if (negativeSemanticOrientations != null
					&& negativeSemanticOrientations[i] != null) {
					semanticOrientation = formatter.format(negativeSemanticOrientations[i]);
				} else {
					semanticOrientation = "?";
				}
				
				result = matcher.replaceAll("<span style=\"background-color: red; color: white;\">"
					+ phrase + "<span style=\"vertical-align: super; font-size: x-small;\"><i>" + semanticOrientation + "</i>&nbsp;</span></span>");
			}										
		}
		
		return result;
	}
	
	public static DataTransferObject getDataTransferObject(Data data, DataSourceTransferObject dataSource) {
		DataTransferObject dataTransferObject = new DataTransferObject();
		dataTransferObject.setId(data.getId());
		dataTransferObject.setDataSource(dataSource);
		dataTransferObject.setHeading(data.getHeading());
		dataTransferObject.setEvaluation(data.getEvaluation());
		dataTransferObject.setText(data.getText().getValue());
		dataTransferObject.setHTML(getHTML(data.getText().getValue(), data.getNeutralPhrases(), data.getPositivePhrases(),
			data.getPositiveSemanticOrientations(), data.getNegativePhrases(), data.getNegativeSemanticOrientations()));
		dataTransferObject.setTime(data.getTime());
		dataTransferObject.setSkipped(data.getSkipped());
		dataTransferObject.setNeutralPhrases(data.getNeutralPhrases());
		dataTransferObject.setPositivePhrases(data.getPositivePhrases());
		dataTransferObject.setPositiveSemanticOrientations(data.getPositiveSemanticOrientations());
		dataTransferObject.setNegativePhrases(data.getNegativePhrases());
		dataTransferObject.setNegativeSemanticOrientations(data.getNegativeSemanticOrientations());
		
		return dataTransferObject;
	}
	
	public static DataSourceTransferObject getDataSourceTransferObject(DataSource dataSource) {
		DataSourceTransferObject dataSourceTransferObject = new DataSourceTransferObject();
		dataSourceTransferObject.setId(dataSource.getId());
		dataSourceTransferObject.setName(dataSource.getName());
		dataSourceTransferObject.setUrl(dataSource.getUrl());
		dataSourceTransferObject.setFeedUrl(dataSource.getFeedUrl());
		dataSourceTransferObject.setEnabled(dataSource.getEnabled());
		
		return dataSourceTransferObject;
	}
	
	public static Data getData(DataTransferObject dataTransferObject) {
		Data data = new Data();
		data.setId(dataTransferObject.getId());
		data.setDataSourceId(dataTransferObject.getDataSource().getId());
		data.setHeading(dataTransferObject.getHeading());
		data.setText(new Text(dataTransferObject.getText()));
		data.setEvaluation(dataTransferObject.getEvaluation());
		data.setTime(dataTransferObject.getTime());
		data.setSkipped(dataTransferObject.getSkipped());
		data.setNeutralPhrases(dataTransferObject.getNeutralPhrases());
		data.setPositivePhrases(dataTransferObject.getPositivePhrases());
		data.setPositiveSemanticOrientations(dataTransferObject.getPositiveSemanticOrientations());
		data.setNegativePhrases(dataTransferObject.getNegativePhrases());
		data.setNegativeSemanticOrientations(dataTransferObject.getNegativeSemanticOrientations());
		
		return data;
	}
	
	public static DataSource getDataSource(DataSourceTransferObject dataSourceTransferObject) {
		DataSource dataSource = new DataSource();
		dataSource.setId(dataSourceTransferObject.getId());
		dataSource.setName(dataSourceTransferObject.getName());
		dataSource.setUrl(dataSourceTransferObject.getUrl());
		dataSource.setFeedUrl(dataSourceTransferObject.getFeedUrl());
		dataSource.setEnabled(dataSourceTransferObject.getEnabled());
		
		return dataSource;
	}
	
	public static boolean isMSIE() {
		return Navigator.getUserAgent().contains("msie");
	}
	
}
